package billing;

import java.io.Serializable;
import java.rmi.RemoteException;

/**
 * Represent a single Price-Step
 * @author Gernot
 *
 */
public class PriceStep implements Serializable, Comparable<Object> {
    private static final long serialVersionUID = -1848667782432224075L;
    private double startPrice;
    private double endPrice;
    private double fixedPrice;
    private double variablePricePercent;
    
    public PriceStep(double startPrice, double endPrice, double fixedPrice,
            double variablePricePercent) throws RemoteException {
        super();
        if (endPrice != 0){
            this.endPrice = endPrice;
        } else {
            this.endPrice = Double.POSITIVE_INFINITY;
        }
        if ((startPrice < 0) || (endPrice <0)  || (this.endPrice <= startPrice)){
            throw new RemoteException("Invalid Price intervall");
        }
        if (fixedPrice <0 || variablePricePercent <0){
            throw new RemoteException("Prices must not be negative");
        }
        
        this.startPrice = startPrice;
        this.fixedPrice = fixedPrice;
        this.variablePricePercent = variablePricePercent;
    }

    public double getStartPrice() {
        return startPrice;
    }

    public void setStartPrice(double startPrice) {
        this.startPrice = startPrice;
    }

    public double getEndPrice() {
        return endPrice;
    }

    public void setEndPrice(double endPrice) {
        if (endPrice != 0){
            this.endPrice = endPrice;
        } else {
            this.endPrice = Double.POSITIVE_INFINITY;
        }
    }

    public double getFixedPrice() {
        return fixedPrice;
    }

    public void setFixedPrice(double fixedPrice) {
        this.fixedPrice = fixedPrice;
    }

    public double getVariablePricePercent() {
        return variablePricePercent;
    }

    /**
     * Returns percent as double 1.0 == 100%
     * @return
     */
    public double getVarialePriceDouble(){
        return variablePricePercent*0.01;
    }
    
    public void setVariablePricePercent(double variablePricePercent) {
        this.variablePricePercent = variablePricePercent;
    }

    @Override
    public String toString() {
        return "("+this.startPrice+"-"+this.endPrice+"]\t"+this.fixedPrice+"\t"+this.variablePricePercent;
    }
    
    /**
     * Returs if passed Intervall is the same as this objects intervall
     * @param startPrice
     * @param endPrice
     * @return
     */
    public boolean isSameIntervall(double startPrice, double endPrice){
        double priceend;
        if (endPrice != 0){
            priceend = endPrice;
        } else {
            priceend = Double.POSITIVE_INFINITY;
        }
        
        return this.startPrice==startPrice && this.endPrice == priceend;
    }
    
    
    /**
     * returns if a price belongs to this PriceSteps intervall
     * @param price
     * @return
     */
    public boolean isInIntervall(double price){
        return (price >= this.startPrice && price <= this.endPrice);
    }
    
    /**
     * Returns if this PriceSteps intervall overlaps with another PriceStep
     * @param other
     * @return
     */
    public boolean overlaps(PriceStep other){
        if (this.endPrice <= other.startPrice || this.startPrice >= other.endPrice){
            return false;
        }
        return true;
    }

    @Override
    public int compareTo(Object arg0) {
        if (arg0 instanceof PriceStep) {
            PriceStep ps = (PriceStep) arg0;
            return new Double(this.startPrice).compareTo(ps.startPrice);
        }
        return 0;
    }
    
}
